<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="java.sql.Connection"%>
<%@page import="com.yuxin.common.util.DBUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="com.yuxin.common.util.CommonUtil"%>
<%
	Connection conn = null;
	List<Map<String, Object>> favoriteList = null;
	String redirect = null;
	TransactionManager tranM = null;
	try {
		if(!CommonUtil.isLoginValid(request)){
			response.sendRedirect(request.getContextPath()+"/site/denglu.jsp");
			return;
		 }
		
		String credit_user_id = CommonUtil.getCookie(request, "TICKET");
		conn = DBUtil.getConnection();
		 // 页面标题
        request.setAttribute("titleName",URLDecoder.decode("我的收藏", "UTF-8"));
		
		String sql = "select t.ID, t.GOODS_ID, t2.TITLE_IMG, t2.TITLE, t2.PRICE from p_favorite t, p_goods t2" 
					+ " where t.CREDIT_ID = ? and t.GOODS_ID = t2.ID order  by t.CREATE_TIME desc";
		favoriteList = DBUtil.queryList(conn, sql, credit_user_id);
		
		// Remove my favorite record
		String action = request.getParameter("action");
		String id = request.getParameter("id");
		if(action != null && "remove".equals(action) && id != null && !"".equals(id)) {
			tranM = new TransactionManager(conn);
			tranM.begin();
			
			sql = "delete from p_favorite where id = ?";
			int res = DBUtil.executeSql(conn, sql, id);			
			if(res > 0) {
				redirect = request.getContextPath()+"/site/my_favorite.jsp?res_code=101";
			}
		}
	} catch (Exception ex) {
		if(tranM != null) {
			tranM.rollback();
		}
		ex.printStackTrace();
	} finally {
		if(tranM != null) {
			tranM.commit();
		}
		DBUtil.close(conn);
		if(redirect != null) {
			response.sendRedirect(redirect);
		}
	}
%>
<!DOCTYPE html>

<%@page import="com.yuxin.core.TransactionManager"%><html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>${titleName}</title>
        <meta name="Keywords" content="中信信用卡平台" />
        <meta name="Description" content="${titleName}"/>
        <meta name="robots" content="all"/>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes"/>
        <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
        <meta name="format-detection" content="telephone=no"/>
        <meta http-equiv="cleartype" content="on"/>
        <link rel="icon" type="image/png" href="images/favicon.ico">
        <%@ include file="/site/css.jsp" %>
        <style type="text/css">
			.biaoge{margin-top:10px;width:100%;background-color:#fff;overflow:hidden;border:1px solid #C6C4C2;line-height:24px;-webkit-border-radius: .2em;-moz-border-radius: .2em;border-radius: .2em;}
			.biaoge li{list-style-type: none;color:#666;padding:1px 10px;border-bottom:1px solid #C6C4C2;line-height:30px;overflow:hidden;}
			.biaoge li:last-child{border-bottom: none;}
			.biaoge .name{float:left;}
			.biaoge .value{float:left;}
		</style>
	</head>
	<body>
        <div id="page">
            <%@ include file="/site/header_nav.jsp" %>
            <div id="container">           	            	
              <%
			  	if(favoriteList == null || favoriteList.size() == 0) {
			  		out.println("<div style='margin-top:10px; text-align:center;'>暂无收藏记录！</div>");
			  	} else {
			   %>
               <ul class="biaoge">		
			 <%		
              		for(Map<String, Object> map : favoriteList) {
			  %>
              <li>
                <div class="name" style="margin:3px; width:40%;"><img src="<%=request.getContextPath() %>/<%=map.get("TITLE_IMG") %>" width="100" /></div>
                <div class="value" style="float:left; width:55%;">
                    <div><%=map.get("TITLE") %></div> 
                    <div>&nbsp;</div>
                    <div><%=map.get("PRICE") %></div>
                </div>
                <div style="width:100%; padding:10px;">
                	<a class="button big blue" href="my_favorite.jsp?action=remove&amp;id=<%=map.get("ID") %>">移除</a>
                    <div style="height:10px;"></div>
                    <a class="button big orange" onClick="addToCart('<%=map.get("GOODS_ID") %>');">加入购物车</a>
                 </div>
              </li>
            <%
				}
			%>
            </ul>	
			<%	
			  }	
            %>
            </div>
            <%@ include file="/site/footer.jsp" %>
        </div>
    </body>
     <%@ include file="/site/js.jsp" %>
    <script>
		$(document).ready(function(e) {
            var res_code = "${param.res_code}";
			if(res_code != '') {
				if(res_code == "101") {
					appUtil.alert("商品已移除！");									
				}	
			}
        });
		
		 function addToCart(goods_id){
			var cookieStr = appUtil.getCookie("productList",{"path":"/site"}) || "{}";
			var productList = JSON.parse(cookieStr);
			if(productList[goods_id]){
				productList[goods_id] = productList[goods_id]*1 + 1;
			}else{
				productList[goods_id] = "1";
			}
			appUtil.setCookie("productList",JSON.stringify(productList),{"path":"${ctx}/site"});
			appUtil.alert("添加成功！");
			return false;
		}
    </script>
</html>